<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="农户名称" prop="farmNm">
        <el-input
          v-model="queryParams.farmNm"
          placeholder="请输入农户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="农户识别号" prop="farmId">
        <el-input
          v-model="queryParams.farmId"
          placeholder="请输入农户识别号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="农户地址" prop="farmAddr">
        <el-input
          v-model="queryParams.farmAddr"
          placeholder="请输入农户地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="农户电话" prop="mobileNo">
        <el-input
          v-model="queryParams.mobileNo"
          placeholder="请输入农户电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="所属行政区划" prop="addvCd">
        <el-input
          v-model="queryParams.addvCd"
          placeholder="请输入所属行政区划"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="所属机构" prop="orgCd">
        <treeselect
            v-model="queryParams.orgCd"
            :options="xzqhOptions"
            :normalizer="normalizer"
            :show-count="true"
            placeholder="水管机构"
            @select="selectAd"
          />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['datam:WiFarmerB:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['datam:WiFarmerB:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['datam:WiFarmerB:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['datam:WiFarmerB:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="WiFarmerBList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="农户编码" align="center" prop="farmCd" /> -->
      <el-table-column label="农户名称" align="left" prop="farmNm">
      </el-table-column>
      <!-- <el-table-column label="农户识别号" align="center" prop="farmCd">
      </el-table-column> -->
      <el-table-column label="农户地址" min-width="180" show-overflow-tooltip align="left" prop="farmAddr">
      </el-table-column>
      <el-table-column label="农户电话" align="center" prop="mobileNo">
      </el-table-column>
      <!-- <el-table-column label="所属行政区划" align="center" prop="addvCd">
      </el-table-column> -->
      <el-table-column label="所属地块" align="center" prop="landNm">
      </el-table-column>
      <el-table-column label="所属机构" align="center" prop="orgNm">
      </el-table-column>
      <el-table-column label="当前状态" align="center" prop="farmStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.farm_status" :value="scope.row.farmStatus"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作人员" align="center" prop="farmOper">
      </el-table-column> -->
      <!-- <el-table-column label="备注" align="center" prop="farmMark">
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['datam:WiFarmerB:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['datam:WiFarmerB:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改农户信息登记对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :inline="true" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="农户名称" prop="farmNm">
          <el-input class="w202" v-model="form.farmNm" placeholder="请输入农户名称" />
        </el-form-item>
        <!-- <el-form-item label="农户识别号" prop="farmId">
          <el-input v-model="form.farmId" placeholder="请输入农户识别号" />
        </el-form-item> -->
        <el-form-item label="农户地址" prop="farmAddr">
          <el-input class="w202" v-model="form.farmAddr" placeholder="请输入农户地址" />
        </el-form-item>
        <el-form-item label="农户电话" prop="mobileNo">
          <el-input class="w202" v-model="form.mobileNo" placeholder="请输入农户电话" />
        </el-form-item>
        <!-- <el-form-item label="所属行政区划" prop="addvCd">
          <el-input v-model="form.addvCd" placeholder="请输入所属行政区划" />
        </el-form-item> -->

        <el-form-item label="所属机构" prop="orgCd">
          <treeselect
            v-model="form.orgCd"
            :options="xzqhOptions"
            :normalizer="normalizer"
            :show-count="true"
            placeholder="水管机构"
            @select="selectAd"
          />
        </el-form-item>
        <el-form-item label="当前状态" prop="farmStatus">
          <el-select class="w202" v-model="form.farmStatus" placeholder="请选择农户状态">
            <el-option
              v-for="dict in dict.type.farm_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="操作人员" prop="farmOper">
          <el-input class="w202" v-model="form.farmOper" placeholder="请输入操作人员" />
        </el-form-item>
        <el-form-item label="备注" prop="farmMark">
          <el-input class="w202" v-model="form.farmMark" placeholder="请输入备注" />
        </el-form-item>
        <el-row>
          <el-col span="24">
            <el-form-item label="对应地块" prop="landCd">
              <treeselect
                :multiple="true"
                v-model="form.landCd"
                :options="landData"
                :normalizer="normalizer2"
                :show-count="true"
                search-nested
                placeholder="对应地块"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWiFarmerB, getWiFarmerB, delWiFarmerB, addWiFarmerB, updateWiFarmerB } from "@/api/datam/WiFarmerB";
import { listAllWiLandB } from "@/api/datam/WiLandB";
import { organtreeList } from "@/api/common";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  components:{Treeselect},
  name: "WiFarmerB",
  dicts: ["farm_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 农户信息登记表格数据
      WiFarmerBList: [],
      xzqhOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        farmNm: null,
        farmId: null,
        farmAddr: null,
        mobileNo: null,
        addvCd: null,
        orgCd: null,
        farmStatus: null,
        farmOper: null,
        farmMark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        farmNm : [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        mobileNo: [
          { required: true,
            type: 'number',
            message: '手机号格式不正确',
            trigger: 'blur',
            transform(value) {
              var phonereg = 11 && /^((13|14|15|16|17|18|19)[0-9]{1}\d{8})$/
              if (!phonereg.test(value)) {
                return false
              } else {
                return Number(value)
              }
            }
          }
        ],
      },
      landData:[]
    };
  },
  created() {
    this.getTreeselect();
    this.getLandList();
    this.getList();
  },
  methods: {
    /** 查询地块列表 */
    getLandList() {
      this.loading = true;
      listAllWiLandB(this.queryParams).then((response) => {
        this.landData = response.rows;
      });
    },
    /** 查询行政区域下拉树结构 */
    getTreeselect() {
        organtreeList().then(response => {
            this.xzqhOptions = this.handleTree(response.data,"orgCd","parentOrgCd");
        });
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.orgCd,
        label: node.orgNm,
        children: node.children,
      };
    },
    /** 转换菜单数据结构 */
    normalizer2(node) {
      return {
        id: node.landCd,
        label: node.landNm,
      };
    },
    // 获取区划名称
    selectAd(val) {
      // this.queryParams.tableTitle = val.orgNm
      // this.getlistWiFarmer()
    },
    /** 查询农户信息登记列表 */
    getList() {
      this.loading = true;
      listWiFarmerB(this.queryParams).then(response => {
        this.WiFarmerBList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        farmCd: null,
        farmNm: null,
        farmId: null,
        farmAddr: null,
        mobileNo: null,
        addvCd: null,
        orgCd: null,
        landCd: null,
        farmStatus: null,
        farmOper: null,
        farmMark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.farmCd)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加农户信息登记";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const farmCd = row.farmCd || this.ids
      getWiFarmerB(farmCd).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改农户信息登记";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.farmCd != null) {
            updateWiFarmerB(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWiFarmerB(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const farmCds = row.farmCd || this.ids;
      this.$modal.confirm('是否确认删除农户信息登记编号为"' + farmCds + '"的数据项？').then(function() {
        return delWiFarmerB(farmCds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('datam/WiFarmerB/export', {
        ...this.queryParams
      }, `用水户管理_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
